<?

include_once ("Server.php");
include_once ("config.php");

class UserRepo extends BaseRepo{

	protected function getEntityName(){
		return "User";
	}

	public function findUser($userName, $password)
	{
		$userName = stripslashes($userName);
		$password = stripslashes($password);
		$con = connect();;
		$userName = mysql_real_escape_string($userName, $con);
		$password = mysql_real_escape_string($password, $con);
		disconnect($con);
		$q="WHERE name='".$userName."' AND password='".$password."'";
		return $this->get($q);
	}
	
	public function getByName($userName){
		$q="WHERE name='".$userName."'";
		$users = $this->get($q);
		return $users[0];
	}

	public function getUserFromTeam($teamID)
	{
		$con = connect();
		$q="SELECT u.id,u.name,u.role,u.email,u.phone_number,u.country_code,u.provider FROM user u ";
		$q = $q."JOIN team t ON t.user_id=u.id ";
		$q = $q."WHERE t.id=".$teamID;
		$result= query($q, $con);
		if (mysql_num_rows($result) > 0)
		{
			$userRow =  mysql_fetch_array($result);
			$user = new User($userRow['id'],$userRow['name'],$userRow['role']);
			$user->setContactInfo($userRow['email'],$userRow['phone_number'],$userRow['country_code'],$userRow['provider']);
		}
		return $user;
	}

	public function getAllUsersToSeeUpdates($draftID)
	{
		$con = connect();
		$q="SELECT u.id,u.name,u.role,u.email,u.phone_number,u.country_code,u.provider FROM user u ";
		$q = $q."JOIN team t ON t.user_id=u.id ";
		$q = $q."WHERE u.send_me_updates = 'y' AND t.draft_id=".$draftID;
		$rows = query($q, $con);
		if (mysql_num_rows($rows) > 0)
		{
			$retVal = array();
			while ($userRow = mysql_fetch_array($rows))
			{
				$user = new User($userRow['id'],$userRow['name'],$userRow['role']);
				$user->setContactInfo($userRow['email'],$userRow['phone_number'],$userRow['country_code'],$userRow['provider']);
				$retVal[] = $user;
			}
		}
		return $retVal;
	}
}

?>